﻿using STF.Blog.Common;
using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class AdminsService : IAdminsService
    {
        private IAdminsManager _iam;
        public AdminsService(IAdminsManager iam)
        {
            _iam = iam;
        }
        public async Task<int> AddUsersAsync(string account, string pwd, string nickname, DateTime birthday, Gender gender, string photo, string tel, string email, string qqnumber, string address, int rolesId)
        {
            return await _iam.AddAsync(new Model.Admins 
            {
                Account = account,
                Pwd = pwd,
                NickName = nickname,
                Birthday = birthday,
                Gender = gender,
                Photo = photo,
                Tel = tel,
                Email = email,
                QQNumber = qqnumber,
                Address = address,
                RolesId = rolesId
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _iam.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _iam.DeleteAsync(data);
        }

        public async Task<List<AdminDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync(ad => ad.DeleteId == deleteId);
            return data == null ? new List<AdminDto>() : data.Select(ad => new AdminDto 
            {
                Id = ad.Id,
                Account = ad.Account,
                Pwd = ad.Pwd,
                NickName = ad.NickName,
                Birthday = ad.Birthday,
                Gender = ad.Gender,
                Photo = ad.Photo,
                Tel = ad.Tel,
                Email = ad.Email,
                QQNumber = ad.QQNumber,
                Address = ad.Address,
                RolesId = ad.RolesId,
                CreateTime = ad.CreateTime,
                UpdateTime = ad.UpdateTime
            }).ToList();
        }

        public async Task<AdminDto> GetByIdAsync(int id)
        {
            var data = await _iam.GetById(id);
            return data == null? new AdminDto() : new AdminDto
            {
                Id = data.Id,
                Account = data.Account,
                Pwd = data.Pwd,
                NickName = data.NickName,
                Birthday = data.Birthday,
                Gender = data.Gender,
                Photo = data.Photo,
                Tel = data.Tel,
                Email = data.Email,
                QQNumber = data.QQNumber,
                Address = data.Address,
                RolesId = data.RolesId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<AdminDto>> GetByNameAsync(string nickname, int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync(ad => ad.NickName.Contains(nickname) && ad.DeleteId == deleteId);
            return data == null ? new List<AdminDto>() : data.Select(ad => new AdminDto 
            {
                Id = ad.Id,
                Account = ad.Account,
                Pwd = ad.Pwd,
                NickName = ad.NickName,
                Birthday = ad.Birthday,
                Gender = ad.Gender,
                Photo = ad.Photo,
                Tel = ad.Tel,
                Email = ad.Email,
                QQNumber = ad.QQNumber,
                Address = ad.Address,
                RolesId = ad.RolesId,
                CreateTime = ad.CreateTime,
                UpdateTime = ad.UpdateTime
            }).ToList();
        }

        public async Task<AdminDto> LoginAsync(string email, string pwd)
        {
            var data = await _iam.GetChooseAsync(a => a.Email.Equals(email) && a.Pwd.Equals(pwd));

            if (data.Any())
            {
                var model = data.First();
                return new AdminDto
                {
                    Id = model.Id,
                    NickName = model.NickName,
                    Birthday = model.Birthday,
                    Gender = model.Gender,
                    Tel = model.Tel,
                    Email = model.Email,
                    QQNumber = model.QQNumber,
                    Address = model.Address,
                    Photo = model.Photo,
                    RolesId = model.RolesId,
                    Pwd = model.Pwd,
                    UpdateTime = model.UpdateTime
                };
            }
            else 
            {
                return null;
            }
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _iam.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _iam.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _iam.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 1;
            return await _iam.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string pwd, string nickname, DateTime birthday, Gender gender, string photo, string tel, string email, string qqnumber, string address, int rolesId)
        {
            var data = await _iam.GetById(id);
            if (data == null)
                return -1;
            data.Pwd = pwd;
            data.NickName = nickname;
            data.Birthday = birthday;
            data.Gender = gender;
            data.Photo = photo;
            data.Tel = tel;
            data.Email = email;
            data.QQNumber = qqnumber;
            data.Address = address;
            data.RolesId = rolesId;
            data.UpdateTime = DateTime.Now;
            return await _iam.UpdateAsync(data);
        }
    }
}
